package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.OmsOrderDao;
import com.macro.mall.dao.OmsOrderOperateHistoryDao;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.OmsOrderMapper;
import com.macro.mall.mapper.OmsOrderOperateHistoryMapper;
import com.macro.mall.model.OmsOrder;
import com.macro.mall.model.OmsOrderExample;
import com.macro.mall.model.OmsOrderOperateHistory;
import com.macro.mall.service.OmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CmsSubjectServiceImpl
 * @Description 订单管理Service实现类
 * @Author lin.dai
 * @Version 1.0
 **/
@Service
public class OmsOrderServiceImpl implements OmsOrderService {
    @Autowired
    private OmsOrderMapper omsOrderMapper;

    @Autowired
    private OmsOrderDao omsOrderDao;

    @Autowired
    private OmsOrderOperateHistoryDao orderOperateHistoryDao;

    @Autowired
    private OmsOrderOperateHistoryMapper omsOrderOperateHistoryMapper;

    @Override
    public List<OmsOrder> list(OmsOrderQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return omsOrderDao.getList(queryParam);
    }

    @Override
    public int delivery(List<OmsOrderDeliveryParam> deliveryParamList) {
        //批量发货，修改
        int count = omsOrderDao.delivery(deliveryParamList);
        //添加操作记录
        //传统写法
        /*ArrayList<OmsOrderOperateHistory> operateHistoryList = new ArrayList<>();
        for (OmsOrderDeliveryParam omsOrderDeliveryParam : deliveryParamList) {
            OmsOrderOperateHistory omsOrderOperateHistory = new OmsOrderOperateHistory();
            omsOrderOperateHistory.setOrderId(omsOrderDeliveryParam.getOrderId());
            ......
            operateHistoryList.add(omsOrderOperateHistory);
        }*/
        //java8写法
        List<OmsOrderOperateHistory> operateHistoryList = deliveryParamList.stream().map(OmsOrderDeliveryParam->{
            OmsOrderOperateHistory history = new OmsOrderOperateHistory();
            history.setOrderId(OmsOrderDeliveryParam.getOrderId());
            history.setCreateTime(new Date());
            history.setOperateMan("后台管理员");
            history.setOrderStatus(2);
            history.setNote("完成发货");
            return  history;
        }).collect(Collectors.toList());
        orderOperateHistoryDao.insertList(operateHistoryList);
        return count;
    }

    @Override
    public int close(List<Long> ids, String note) {
        OmsOrder record = new OmsOrder();
        record.setStatus(4);
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andDeleteStatusEqualTo(0).andIdIn(ids);
        //updateByExampleSelective是更新一条数据中的某些属性，而不是更新整条数据。
        int count = omsOrderMapper.updateByExampleSelective(record, example);
        List<OmsOrderOperateHistory> historyList = ids.stream().map(orderId -> {
            OmsOrderOperateHistory history = new OmsOrderOperateHistory();
            history.setOrderId(orderId);
            history.setCreateTime(new Date());
            history.setOperateMan("后台管理员");
            history.setOrderStatus(4);
            history.setNote("订单关闭:"+note);
            return history;
        }).collect(Collectors.toList());
        return count;
    }

    @Override
    public int delete(List<Long> ids) {
        OmsOrder record = new OmsOrder();
        record.setDeleteStatus(1);
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andDeleteStatusEqualTo(0).andIdIn(ids);
        return omsOrderMapper.updateByExampleSelective(record, example);
    }

    @Override
    public OmsOrderDetail detail(Long id) {
        return omsOrderDao.getDetail(id);
    }

    @Override
    public int updateReceiverInfo(OmsReceiverInfoParam omsReceiverInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(omsReceiverInfoParam.getOrderId());
        order.setReceiverName(omsReceiverInfoParam.getReceiverName());
        order.setReceiverPhone(omsReceiverInfoParam.getReceiverPhone());
        order.setReceiverPostCode(omsReceiverInfoParam.getReceiverPostCode());
        order.setReceiverDetailAddress(omsReceiverInfoParam.getReceiverDetailAddress());
        order.setReceiverProvince(omsReceiverInfoParam.getReceiverProvince());
        order.setReceiverCity(omsReceiverInfoParam.getReceiverCity());
        order.setReceiverRegion(omsReceiverInfoParam.getReceiverRegion());
        order.setModifyTime(new Date());
        int count = omsOrderMapper.updateByPrimaryKeySelective(order);
        //插入操作记录（单条不需要java8写法）
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(omsReceiverInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(omsReceiverInfoParam.getStatus());
        history.setNote("修改收货人信息");
        omsOrderOperateHistoryMapper.insert(history);
        return count;
    }

    @Override
    public int updateMoneyInfo(OmsMoneyInfoParam omsMoneyInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(omsMoneyInfoParam.getOrderId());
        order.setFreightAmount(omsMoneyInfoParam.getFreightAmount());
        order.setDiscountAmount(omsMoneyInfoParam.getDiscountAmount());
        order.setModifyTime(new Date());
        int count = omsOrderMapper.updateByPrimaryKeySelective(order);
        //插入操作记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(omsMoneyInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(omsMoneyInfoParam.getStatus());
        history.setNote("修改费用信息");
        omsOrderOperateHistoryMapper.insert(history);
        return count;
    }

    @Override
    public int updateNote(Long id, String note, Integer status) {
        OmsOrder order = new OmsOrder();
        order.setId(id);
        order.setNote(note);
        order.setModifyTime(new Date());
        int count = omsOrderMapper.updateByPrimaryKeySelective(order);
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(id);
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(status);
        history.setNote("修改备注信息："+note);
        omsOrderOperateHistoryMapper.insert(history);
        return count;
    }
}
